package org.travel.app.configure;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.List;

/**
 * @author nmm
 * @date 2024-11-2024/11/20
 * @deriction njm......
 */
@Configuration
public class RedisConfigure {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) {
    	//创建实例
        RedisTemplate<String,Object> template=new RedisTemplate<>();
        //初始化template
        template.setConnectionFactory(factory);

        //装配序列化器
        //key使用字符串序列化器
        StringRedisSerializer keySerializer=new StringRedisSerializer();
        template.setKeySerializer(keySerializer);
        template.setHashKeySerializer(keySerializer);

        //value使用jackson 序列化器
        GenericJackson2JsonRedisSerializer valuesSerializer=new GenericJackson2JsonRedisSerializer();
        template.setValueSerializer(valuesSerializer);
        template.setHashValueSerializer(valuesSerializer);
        return template;
    }

    @Bean
    public RedisTemplate<String, List<String>> listTemplate(RedisConnectionFactory factory) {
        //创建实例
        RedisTemplate<String,List<String>> template=new RedisTemplate<>();
        //初始化template
        template.setConnectionFactory(factory);

        //装配序列化器
        //key使用字符串序列化器
        StringRedisSerializer keySerializer=new StringRedisSerializer();
        template.setKeySerializer(keySerializer);
        template.setHashKeySerializer(keySerializer);

        //value使用jackson 序列化器
        GenericJackson2JsonRedisSerializer valuesSerializer=new GenericJackson2JsonRedisSerializer();
        template.setValueSerializer(valuesSerializer);
        template.setHashValueSerializer(valuesSerializer);
        return template;
    }

}